package com.atguigu.juc.sync;

/**
 * @author yanglin
 * @create 2024-01-31 21:52 星期三
 * description:
 */
public class SaleTicketDemo {

    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                ticket.sale();
            }
        },"t1").start();
        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                ticket.sale();
            }
        },"t2").start();
        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                ticket.sale();
            }
        },"t3").start();
    }
    
}


class Ticket {    //资源类，模拟三个售票员卖完50张票
    private Integer number = 50;
    
    Object lockObject = new Object();
    
    public void sale(){
        synchronized (lockObject){
            if (number > 0) {
                System.out.println(Thread.currentThread().getName() + "\t 卖出第：\t" + (number--) + "\t 还剩" + number);
            }
        }
    }
}
